#! /usr/bin/python

try:
    import os, sys, urllib
    from random import shuffle
    from datetime import datetime
    sys.path.append('/home/sunyayuan/codebase/taras/')
    sys.path.append('/home/sunyayuan/codebase/')
    sys.path.append('/home/sunyayuan/codebase/sdk/weibopy/')
    import api_adapter
    #reload(sys)
    #sys.setdefaultencoding('gbk')

    print "Content-type: text/html\n\n"

    query = os.environ['QUERY_STRING']
    query = dict(i.split('=') for i in query.split('&'))

    target_account_id = query['target_account_id']
    target_account_id = int(target_account_id)

    minimum_followers_count = query['minimum_followers_count']
    minimum_followers_count = int(minimum_followers_count)

    maximum_followers_count = query['maximum_followers_count']
    maximum_followers_count= int(maximum_followers_count)

    maximum_follow_count = query['maximum_followers_count']
    maximum_follow_count = int(maximum_follow_count)

    minimum_post_count = query['minimum_post_count']
    minimum_post_count = int(minimum_post_count)

    minimum_register_days = query['minimum_register_days']
    minimum_register_days = int(minimum_register_days)

    maximum_register_days = query['maximum_register_days']
    maximum_register_days = int(maximum_register_days)

    user_city = query['user_location']
    user_city = urllib.unquote(user_city).decode('utf-8')
    user_city = user_city.encode('utf-8')
except Exception, err:
    print 'get param failed: %s' % err
    sys.exit()

try:
    SinaApi = api_adapter.create_adapted_api("sina")
    api = SinaApi("722861218", "1cfbec16db00cac0a3ad393a3e21f144")
    import sdk.weibopy.oauth
    token = sdk.weibopy.oauth.OAuthToken('fa473fbdc1d8b736e18a72f2ccad07d3',
                                         'baac261ce0698aef8cfb5b35bdd79b7a')

    api.api = api.create_api_from_token(token)
except Exception, err:
    print 'assign user failed: %s' % err
    sys.exit()

try:    
    followers_id = api.api.complete_followers_ids_list(user_id=target_account_id)
    shuffle(followers_id)
    target_user_string = ''
except Exception, err:
    print 'get followers id for user(%s) failed: %s' % (target_account_id, err)
    sys.exit()

get_number = 0
for follower_id in followers_id:
    try:
        if get_number == 50:
            break
        user_info = api.api.get_user(follower_id)
    except Exception, err:
        continue

    user_screen_name = user_info.screen_name
    user_screen_name = user_screen_name.encode('utf-8')

    user_location = user_info.location
    user_location = user_location.encode('utf-8')

    now = datetime.now()
    user_created_at = user_info.created_at
    user_register_days = (now - user_created_at).days

    #print 'screen name: %s' % user_screen_name
    #print 'followers count: %s' % user_info.followers_count
    #print 'friends count: %s' % user_info.friends_count
    #print 'status count: %s' % user_info.statuses_count
    #print 'register days: %s' % user_register_days
    #print 'location: %s' % user_location
    #print 'gender: %s' % user_info.gender

    if user_info.followers_count >= minimum_followers_count \
    and user_info.followers_count <= maximum_followers_count \
    and user_info.friends_count <= maximum_follow_count \
    and user_info.statuses_count >= minimum_post_count \
    and user_register_days >= minimum_register_days \
    and user_register_days <= maximum_register_days \
    and user_city in user_location:
        get_number += 1
        target_user_string = target_user_string + '@' + user_screen_name
print target_user_string
